/*
 * @Author: @yzcheng
 * @Date: 2021-02-25 18:42:14
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description:
 * @LastEditTime: 2021-03-08 14:08:26
 */
import {
    delBpmn,
    getBpmnById,
    getBpmnList,
    saveBpmnXML,
    updateBpmnById,
    requestUserList,
    requestRoleList,
    requestOrgList,
    requestFlowType,
    requestDeploymentProcess,
    requestListenersList,
    requestFormTmpList, requestAuthoritiesList, requestModelFormList

} from '@/api/bpmnApi'
// import { ElMessage } from 'element-plus'
export default {
    namespaced: true,
    status: () => ({
        modeler: null,
        roleList: [],
        orgList: [],
        userList: [],
        listenersList: null,
        formTmpList: null,
        modelFormList: null,
        authoritiesList: [
            {
                key: "1",
                label: "添加"
            },
            {
                key: "2",
                label: "修改"
            },
            {
                key: "3",
                label: "删除"
            },
            {
                key: "4",
                label: "删除"
            },
            {
                key: "5",
                label: "删除"
            },
        ]
    }),
    state: () => ({
        form: {
            flowType: '',
            flowName: '',
        },
        params: {
            pageNum: 1,
            pageSize: 10,
        },
        rstData:[],
        flowListData:[],
        total:"",
    }),
    getter: {},
    actions: {
        // 获取流程列表
        async getFlowList(context, payload) {
            let rst = await getBpmnList(payload)
            rst = rst.data
            console.log(rst,'getBpmnList');
            if (rst?.success) {
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 保存流程信息
        async saveBpmnXML(context, payload) {
            let rst = await saveBpmnXML(payload);
            rst = rst.data
            console.log(rst,'saveBpmnXML');
            if (rst?.success) {
                return {
                    status: true
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 删除流程
        async delFlowById(context, payload) {
            let rst = await delBpmn(payload)
            rst = rst.data
            console.log(rst,'saveBpmnXML');
            if (rst?.success) {
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 获取某流程详细信息
        async getBpmnXML(context, payload) {
            const rst = await getBpmnById(payload)
            if (rst?.data.success) {
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 更新bpmn xml
        async updateBpmn(context, payload) {
            let rst = await updateBpmnById(payload)
            rst = rst.data
            if (rst?.success) {
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        async getFlowTypeList({ commit }, payload) {
            let rst = await requestFlowType(payload)
            rst = rst.data
            if (rst?.success) {
                commit('saveTypeList', rst.data);
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 获取用户信息列表
        async getUserList({ state }, payload) {
            let rst = await requestUserList(payload)
            rst = rst.data
            if (rst?.success) {
                const data = rst.data.records.map(item => ({
                    label: item.cname,
                    id: item.userId,
                    value: item.username,
                    cname: item.cname,
                    key: item.username
                })
                );
                return {
                    status: true,
                    data,
                    page: {
                        total: rst.data.total,
                        current: rst.data.current
                    }
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 获取角色信息列表
        async getRoleList({ commit }, payload) {
            let rst = await requestRoleList({},payload)
            rst = rst.data
            if (rst?.code==200) {
                const data = rst.data.map(item => ({
                    label: item.roleName,
                    id: item.roleId,
                    leaf: true,
                    key:item.roleId
                })
                );
                commit('saveRoleList', data);
                return {
                    status: true,
                    data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 获取机构列表
        async getOrgList({ commit }, payload) {
            let rst = await requestOrgList(payload)
            rst = rst.data
            if (rst?.success) {
                commit('saveOrgList', rst.data);
                return {
                    status: true,
                    data: rst.data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        // 部署流程
        async depolymentProcess(context, payload) {
            let rst = await requestDeploymentProcess(payload)
            rst = rst.data
            if (rst?.success) {
                return {
                    status: true,
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        async getListenersList({ commit }, payload) {
            let rst = await requestListenersList(payload);
            rst = rst.data
            const listenerGroup = [
                {
                    label: "事件监听器",
                    options: [],
                },
                {
                    label: "执行监听器",
                    options: [],
                },
                {
                    label: "任务监听器",
                    options: [],
                }
            ];
            if (rst?.success) {
                rst.data.forEach(item => {
                    listenerGroup[item.type - 1].options.push(
                        Object.assign(item, {
                            label: item.name,
                            value: item.id
                        }))
                });
                commit('saveListenersList', listenerGroup);
                return {
                    status: true,
                    data: listenerGroup
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        async getFormTmpList({ commit }, payload) {
            let rst = await requestFormTmpList(payload)
            rst = rst.data
            if (rst?.code === 200) {
                commit('saveFormTmpList', rst.data.nodes);
                return {
                    status: true,
                    data: rst.data.nodes
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        async getModelFormList({ commit }, payload) {
            let rst = await requestModelFormList(payload)
            rst = rst.data
            if (rst?.code === 200) {
                commit('saveModelFormList', rst.data.nodes);
                return {
                    status: true,
                    data: rst.data.nodes
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        },

        async getAuthoritiesList({ commit }, payload) {
            let rst = await requestAuthoritiesList(payload)
            rst = rst.data
            if (rst?.success) {
                const data = rst.data.map(item => ({
                    key: item.id,
                    label: item.name
                }))
                commit('saveAuthoritiesList', data);
                return {
                    status: true,
                    data
                }
            } else {
                return {
                    status: false,
                    message: rst?.message
                }
            }
        }

    },
    mutations: {
        updateBpmnModeler(state, modeler) {
            state.modeler = modeler
        },
        saveRoleList(state, payload) {
            state.roleList = payload;
        },
        saveOrgList(state, payload) {
            state.orgList = payload;
        },
        saveTypeList(state, payload) {
            state.typeList = payload
        },
        saveListenersList(state, payload) {
            state.listenersList = payload
        },
        saveFormTmpList(state, payload) {
            state.formTmpList = payload;
        },
        saveModelFormList(state, payload) {
            state.modelFormList = payload
        },
        saveAuthoritiesList(state, payload) {
            state.authoritiesList = payload;
        }
    },
}
